package com.wl.java_spring_boot01.modules.test.service.impl;



import com.wl.java_spring_boot01.modules.test.dao.CountryDao;
import com.wl.java_spring_boot01.modules.test.entity.Country;
import com.wl.java_spring_boot01.modules.test.service.CountryServcie;
import com.wl.java_spring_boot01.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description CountryServcieImpl
 * @Author HymanHu
 * @Date 2020/8/11 13:59
 */
@Service
public class CountryServcieImpl implements CountryServcie {

    @Autowired
    private CountryDao countryDao;

    //2.2注入RedisTemplate
    /*@Autowired
    private RedisTemplate redisTemplate;*/
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Country getCountryByCountryId(int countryId) {
        return countryDao.getCountryByCountryId(countryId);
    }

    //CountryName查询国家
    @Override
    public Country getCountryByCountryName(String countryName) {
        return countryDao.getCountryByCountryName(countryName);
    }

    //redis缓存测试
    @Override
    public Country mograteCountryByRedis(int countryId) {
        //1.将contry从数据库拿出
        Country country_db = countryDao.getCountryByCountryId(countryId);
        //2.将country存到redis里面
        //2.1使用countryId标识country
        String countryKey = String.format("country%d",countryId);
        //2.3设置对应的String字符串到redisTemplate
       /* redisTemplate.opsForValue().set(countryKey,country_db);
        return (Country) redisTemplate.opsForValue().get(countryKey);*/
        redisUtils.set(countryKey,country_db);
        return (Country) redisUtils.get(countryKey);
    }

}
